﻿/*
 * instructions：chapter1类在于理顺 私钥、公钥、公钥哈希、比特币地址和ScriptPutKey关系
 * ...
 * ...
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NBitcoin;

namespace ProgrammingBlockchain.Chapters
{
    class Chapter1
    {
        Script record_scriptPubKey = null;
        public void Lesson1()
        {
            /*
             * 生成一个新的私钥
             */
            Key key = new Key();
            /*
             * 获取对应的公钥
             */
            PubKey pubKey = key.PubKey;
            Console.WriteLine("Public Key:{0}\n",pubKey);
            /*
             * 获取对应的公钥哈希值
             */
            KeyId hash = pubKey.Hash;
            Console.WriteLine("Hashed Key:{0}\n", hash);
            /*
             * 获取对应的比特币地址
             */
            BitcoinAddress address = pubKey.GetAddress(Network.Main);
            Console.WriteLine("Address:{0}\n", address);
            /*
             * ScriptPubKey是一个简短的脚本，用于解释在什么情况下才能声明比特币的所有权
             */
            Script scripPubKeyFormAddress = address.ScriptPubKey;
            Console.WriteLine("ScriptPubKey from address:{0}\n", scripPubKeyFormAddress);
            Script scriptPubKeyFromHash = hash.ScriptPubKey;
            Console.WriteLine("ScriptPubKey from hash:{0}\n", scriptPubKeyFromHash);
            /*
             * 本次产生的私钥的ScriptPubKey存于本地
             */
            record_scriptPubKey = scripPubKeyFormAddress;
        }

        public void Lesson2()
        {
            ///*
            // * 可以自定义scriptPubKey值，不过要已经存在的scriptPubKey值
            // */
            //Script scriptPubKey = new Script("OP_DUP OP OP_HASH160" + 
            //    "50507170c878f80000156a2cf85c727148722992" +
            //    "OP_EQUALVERIFY OP_CHECKSIG");
            /*
             * 通过scriptPubKey得到相应的比特币地址
             */
            BitcoinAddress address = record_scriptPubKey.GetDestinationAddress(Network.Main);
            Console.WriteLine("Bitcoin:{0}\n", address);
        }

        public void Lesson3()
        {
            ///*
            // * 可以自定义scriptPubKey值，不过要已经存在的scriptPubKey值
            // */
            //Script scriptPubKey = new Script("OP_DUP OP OP_HASH160" + 
            //    "50507170c878f80000156a2cf85c727148722992" +
            //    "OP_EQUALVERIFY OP_CHECKSIG");
            /*
             * 通过scriptPubKey获取哈希值
             */
            KeyId hash = (KeyId)record_scriptPubKey.GetDestination();
            Console.WriteLine("Public Key Hash:{0}\n", hash);
            /*
             * 通过哈希值产生一个比特币地址
             */
            BitcoinAddress address = new BitcoinPubKeyAddress(hash, Network.Main);
            Console.WriteLine("Bitcoin Address:{0}\n", address);
        }

        public BitcoinSecret Lesson4()
        {
            /*
             * 生成一个新的私钥，返回比特币密码
             */
            Key key = new Key();
            BitcoinSecret secret = key.GetBitcoinSecret(Network.Main);
            Console.WriteLine("Bitcoin Secret:{0}\n", secret);
            return secret;
        }
    }

}
